# -*- coding: utf-8 -*-
from liberLibs.path import Path
from liberLibs import utils, resource
from liberLibs.library import Library


def execute(library, category_dir, name, files, comments, thumbnail):
    """
    if not in dcc software, run this
    :param library: a library name
    :param category_dir: category directory
    :param name: toolset name
    :param files: the file list you want to upload
    :param comments: the comments of this toolset
    :param thumbnail: a thumbnail path
    :return:
    """
    if isinstance(files, basestring):
        files = [files]
    files = list(set(files))
    has_copied_files = copy_files(library, category_dir, name, files)
    if not has_copied_files:
        return False
    thumbnail = str(thumbnail.replace("\\", "/"))
    tool_directory = "%s/%s" % (category_dir, name)
    thumbnail_path = "%s/thumbnail.png" % tool_directory
    thumbnail_path = str(thumbnail_path.replace("\\", "/"))
    info_path = "%s/info.json" % tool_directory
    if thumbnail:
        if thumbnail != thumbnail_path:
            utils.copy(thumbnail, thumbnail_path)
    else:
        if Library(library).auto_thumbnail():
            generate_thumbnail(files, thumbnail_path)
        else:
            utils.copy(resource.icon_file("image.png"), thumbnail_path)
    if not Path(thumbnail_path).isFile():
        utils.copy(resource.icon_file("image.png"), thumbnail_path)
    utils.save_info(comments, info_path)
    return True


def copy_files(library, category_dir, name, files):
    """
    copy the drag files
    :param library: a library name
    :param category_dir: category directory
    :param name: toolset name
    :param files: the files you want to upload
    :return:
    """
    files_num = len(files)
    _lib = Library(library)
    if not _lib.create_ext_folder():
        ext_dir = ""
    else:
        exts = [Path(_file).ext() for _file in files]
        exts = list(set(exts))
        if len(exts) == 1:
            ext_dir = exts[0].split(".")[-1]
            if _lib.support_multi_ext():
                ext_dir = "files"
        else:
            ext_dir = "files"
    for _file in files:
        path_obj = Path(_file)
        if not path_obj.isFile():
            continue
        base_name = path_obj.basename()
        ext = path_obj.ext()
        if files_num == 1:
            new_path = "{category_dir}/{name}/{ext_dir}/{name}{ext}".format(category_dir=category_dir,
                                                                            name=name, ext_dir=ext_dir, ext=ext)
        else:
            new_path = "{category_dir}/{name}/{ext_dir}/{base_name}".format(category_dir=category_dir, name=name,
                                                                            ext_dir=ext_dir, base_name=base_name)
        new_path = new_path.replace("//", "/")
        utils.copy_file(_file, new_path)
    return True


def generate_thumbnail(files, thumbnail_path):
    """
    automatic generate a thumbnail
    :param files: a file list
    :param thumbnail_path: thumbnail path
    :return:
    """
    try:
        utils.automatic_thumbnail(files, str(thumbnail_path))
    except:
        utils.copy(resource.icon_file("image.png"), thumbnail_path)
